﻿using BLL;
using DevComponents.AdvTree;
using DevComponents.DotNetBar.Controls;
using DevExpress.XtraEditors.Filtering.Templates;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.自定义类;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.新增窗体
{
    public partial class 打样单 : Form
    {
        private readonly LXR jiagongchang = new LXR();
        private LXR Kehu = new LXR();
        private BindingList<danjumingxitable> danjumingxitables = new BindingList<danjumingxitable>();
        private ProofingRequirements requirements = new ProofingRequirements();
        public DanjuTable danju { get; set; } = new DanjuTable();
        public FormUseful Useful { get; set; }
        public 打样单()
        {
            InitializeComponent();
            CreateGrid.Create(this.Name, gridView1);
            工艺单据ToolStripMenuItem.SelectedIndex = 0;
            cmbqiankuan.SelectedIndex = 0;
            comhanshui.SelectedIndex = 0;
            try
            {
                gridView1.Columns["Bianhao"].ColumnEdit = ButtonEdit1;
                gridView1.Columns["OrderNum"].ColumnEdit = ButtonEdit2;
                gridView1.Columns["danwei"].ColumnEdit = cmddanwei;
                gridView1.Columns["yanse"].ColumnEdit = colorbtn;
                gridView1.Columns["ColorNum"].ColumnEdit = colorbtn;
                gridView1.Columns["rq"].ColumnEdit = dtEndDate;
                gridView1.Columns["PingzhiYang"].ColumnEdit = cmbPingzhiyang;
                gridView1.IndicatorWidth = 30;
            }
            catch
            {
                var fm = new 配置列(gridView1) { formname = this.Name, Obj = new danjumingxitable() };
                fm.ShowDialog();
            }
        }
        private void 打样工艺单_Load(object sender, EventArgs e)
        {
            if (Useful == FormUseful.新增)
            {
                Init();
            }
            else
            {
                if (Useful == FormUseful.修改)
                {
                    Edit();
                }
                else
                {
                    if (Useful == FormUseful.复制)
                    {
                        Edit();
                        dateEdit1.DateTime = DateTime.Now.Date;
                        txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.打样单, dateEdit1.DateTime, DanjuLeiXing.打样单);
                        Useful = FormUseful.新增;
                    }
                    else
                    {
                        Edit();
                        保存ToolStripMenuItem.Enabled = false;
                    }
                }
            }
        }
        private void Init()
        {
            foreach (Control c in this.groupControl1.Controls)
            {
                if (c is DevComponents.DotNetBar.Controls.TextBoxX || c is DevExpress.XtraEditors.ButtonEdit || c is Sunny.UI.UIRichTextBox||c is TextBox)
                {
                    c.Text = "";
                }
            }

            dateEdit1.DateTime = DateTime.Now;
            txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.打样单, dateEdit1.DateTime, DanjuLeiXing.打样单);
            checkBoxX1.Checked = true;
            checkBoxX2.Checked = true;
            checkBoxX3.Checked = true;
            checkBoxX4.Checked = true;
            danjumingxitables.Clear();
            for (int i = 0; i < 20; i++)
            {
                danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = dateEdit1.DateTime.AddDays(10) });
            }
            gridControl1.DataSource = danjumingxitables;
            gridControl1.RefreshDataSource();
            Useful = FormUseful.新增;
            赋值要求();
        }
        private void 赋值要求()
        {
            requirements = Connect.dbcontext.Queryable<ProofingRequirements>().First(x => x.DocNum == txtdanhao.Text);
            if (requirements == null)
            {
                requirements = new ProofingRequirements();
            }
            txtzhengfangmian.Text = requirements.FrontOrBack;
            txtshuiya.Text = requirements.Hydraulic;
            txttoushi.Text = requirements.MoisturePermeability;
            txtceshibiaozhun.Text = requirements.TestStandards;
            txtqiebian.Text = requirements.Trimming;
            txtshuixi.Text = requirements.Washing;
            txtfangshuixiaoguo.Text = requirements.WaterproofEffect;
            txtkezhong.Text = requirements.Weight;
            txtmenfu.Text = requirements.Width;
            txtgunxing.Text = requirements.Sticktype;
            txtjiaoshui.Text = requirements.Glue;
            txtdibu.Text = requirements.TPU;
            txtwendu.Text = requirements.Temperature;
            txttieheyali.Text = requirements.FitPressure;
            txtshoujuanyali .Text = requirements.RewindingPressure;
            txtshoujuanzhangli.Text = requirements.RewindingTension;
            TxtTopWeight.Text = requirements.TopWeight;
            txtBottomWeight.Text = requirements.BottomWeight;
            txtSeamSlippage.Text = requirements.SeamSlippage;
            txtDetectionmetrics.Text = requirements.DetectionMetrics;
            txtTearStrength.Text = requirements.TestStandards;
            txtOther.Text = requirements.Other;
            txtdivest.Text = requirements.Divest;
            var testdata = Connect.dbcontext.Queryable<TestData>().First(x => x.DocNum == txtdanhao.Text);
            if (testdata != null)
            {
                txtmenfu.Text = testdata.Width;
                txtchengpingkezhong.Text = testdata.Weight;
                txtjiefenghuayi.Text = testdata.SeamSlippage;
                txtshilieqiangdu.Text = testdata.TearStrength;
                txtfangshuixiaoguo.Text = testdata.WaterproofEffect;
                txttestboli.Text = testdata.Divest;
                txttestshuiya.Text = testdata.Hydraulic;
                txttesttoushi.Text = testdata.MoisturePermeability;
                txttestshuixi.Text = testdata.Washing;
            }
        }
        private void Edit()
        {
            txtdanhao.Text = danju.dh;
            dateEdit1.DateTime = danju.rq;
            jiagongchang.BH = danju.ksbh;
            jiagongchang.MC = danju.ksmc;
            txtlianxidianhua.Text = danju.lianxidianhua;
            txtlianxiren.Text = danju.lianxiren;
            //客户名称
            Kehu.MC = danju.ksmc;
            txtkehu.Text = Kehu.MC;
            //客户编号
            Kehu.BH = danju.wuliuBianhao;
            //备注
            txtbeizhu.Text = danju.bz;
            txtheji.Value = danju.je;
            comhanshui.Text = danju.Hanshui;
            cmbqiankuan.Text = danju.Qiankuan;
            comleixing.Text = danju.jiagongleixing;
            danjumingxitables = new BindingList<danjumingxitable>(danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danju.dh));
            for (int i = danjumingxitables.Count; i < 20; i++)
            {
                danjumingxitables.Add(new danjumingxitable() { danhao = danju.dh });
            }
            gridControl1.DataSource = danjumingxitables;
            gridControl1.RefreshDataSource();
            var listhouzhengli = ShengchandanHouzhengliService.GetShengchandanHouzhenglilst(x => x.shengchandanhao == danju.dh);
            if (listhouzhengli.Count > 0)
            {
                if (listhouzhengli.Where(x => x.HouzhengliGongyi == "A").ToList()[0].Value == true)
                {
                    checkBoxX1.Checked = true;
                }
                else
                {
                    checkBoxX1.Checked = false;
                }
                if (listhouzhengli.Where(x => x.HouzhengliGongyi == "B").ToList()[0].Value == true)
                {
                    checkBoxX2.Checked = true;
                }
                else
                {
                    checkBoxX2.Checked = false;
                }
                if (listhouzhengli.Where(x => x.HouzhengliGongyi == "C").ToList()[0].Value == true)
                {
                    checkBoxX3.Checked = true;
                }
                else
                {
                    checkBoxX3.Checked = false;
                }
                if (listhouzhengli.Where(x => x.HouzhengliGongyi == "D").ToList()[0].Value == true)
                {
                    checkBoxX4.Checked = true;
                }
                else
                {
                    checkBoxX4.Checked = false;
                }
            }
            var yaoqius = ShengChanDanHouZhengLiYaoQiuService.GetShengChanDanHouZhengLiYaoQiulst(x => x.ShengChanDanHao == danju.dh);
            赋值要求();
        }

        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var linkman = new LXR() { ZJC = "", MC = "" };
            var fm = new 客户选择() { linkman = linkman };
            fm.ShowDialog();
            Kehu = fm.linkman;
            txtkehu.Text = Kehu.MC;
            txtlianxidianhua.Text = Kehu.DH;
            txtlianxiren.Text = Kehu.Lxr;
        }

        private void txtkehu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var linkman = new LXR() { ZJC = txtkehu.Text, MC = "" };
                var fm = new 客户选择() { linkman = linkman };
                fm.ShowDialog();
                Kehu = fm.linkman;
                txtkehu.Text = Kehu.MC;
                txtlianxidianhua.Text = Kehu.DH;
                txtlianxiren.Text = Kehu.Lxr;
            }
        }
        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new ShengchandanSeLaodu() };
            fm.ShowDialog();
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text });
            gridControl1.RefreshDataSource();
        }

        private void 复制行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //rowindex = gridView1.FocusedRowHandle;
        }

        private void 粘贴行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //CopyRow.Copy<danjumingxitable>(danjumingxitables, rowindex, gridView1, gridView1.FocusedRowHandle);
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!DocmentAssignment())
                {
                    return;
                }
                var listhouzhenli = new List<ShengchandanHouzhengli>
            {
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "A", Value = checkBoxX1.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "B", Value = checkBoxX2.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "C", Value = checkBoxX3.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "D", Value = checkBoxX4.Checked }
            };
                var yaoqius = new List<ShengChanDanHouZhengLiYaoQiu>();

                danju.TotalMishu = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList().Count();
                danjumingxitables.ForEach(x => { x.toupimishu = x.chengpingmishu;x.weishuiheji=x.hanshuidanjia *x.toupimishu ; });
                Connect.dbcontext.BeginTran();
                if (Useful == FormUseful.新增)
                {
                    打样单BLL.保存(danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList(), danju, listhouzhenli);
                    requirements = new ProofingRequirements()
                    {
                        DocNum = txtdanhao.Text,
                        FrontOrBack = txtzhengfangmian.Text,
                        Hydraulic = txtshuiya.Text,
                        MoisturePermeability = txttoushi.Text,
                        TestStandards = txtceshibiaozhun.Text,
                        Trimming = txtqiebian.Text,
                        Washing = txtshuixi.Text,
                        WaterproofEffect = txtfangshuixiaoguo.Text,
                        Weight = txtkezhong.Text,
                        Width = txtmenfu.Text,
                        Sticktype = txtgunxing.Text,
                        Glue = txtjiaoshui.Text,
                        TPU = txtdibu.Text,
                        FitPressure = txttieheyali.Text,
                        RewindingPressure = txtshoujuanyali.Text,
                        RewindingTension = txtshoujuanzhangli.Text,
                        Temperature = txtwendu.Text,
                        HandFeel = txtshougang.Text,
                        BottomWeight = txtBottomWeight.Text,
                        DetectionMetrics = txtDetectionmetrics.Text,
                        Other = txtOther.Text,
                        SeamSlippage = txtSeamSlippage.Text,
                        TearStrength = txtTearStrength.Text,
                        TopWeight = TxtTopWeight.Text,
                       Divest=txtdivest .Text 
                    };
                    Connect.dbcontext.Insertable(requirements).ExecuteCommand();
                }
                else
                {
                    打样单BLL.修改(danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList(), danju, listhouzhenli);
                    if (requirements != null)
                    {
                        requirements.DocNum = txtdanhao.Text;
                        requirements.FrontOrBack = txtzhengfangmian.Text;
                        requirements.Hydraulic = txtshuiya.Text;
                        requirements.MoisturePermeability = txttoushi.Text;
                        requirements.TestStandards = txtceshibiaozhun.Text;
                        requirements.Trimming = txtqiebian.Text;
                        requirements.Washing = txtshuixi.Text;
                        requirements.WaterproofEffect = txtfangshuixiaoguo.Text;
                        requirements.Weight = txtkezhong.Text;
                        requirements.Width = txtmenfu.Text;
                        requirements.Glue = txtjiaoshui.Text;
                        requirements.TPU = txtdibu.Text;
                        requirements.Sticktype = txtgunxing.Text;
                        requirements.Temperature = txtwendu.Text;
                        requirements.FitPressure = txttieheyali.Text;
                        requirements.RewindingPressure = txtshoujuanyali.Text;
                        requirements.RewindingTension = txtshoujuanzhangli.Text;
                        requirements.HandFeel = txtshougang.Text;
                        requirements.BottomWeight = txtBottomWeight.Text;
                        requirements.DetectionMetrics = txtDetectionmetrics.Text;
                        requirements.Other = txtOther.Text;
                        requirements.SeamSlippage = txtSeamSlippage.Text;
                        requirements.TearStrength = txtTearStrength.Text;
                        requirements.TopWeight = TxtTopWeight.Text;
                        Connect.dbcontext.Updateable(requirements).ExecuteCommand();
                    }
                }
                Connect.dbcontext.Deleteable<TestData>(x=>x.DocNum==txtdanhao .Text ).ExecuteCommand ();
                Connect.dbcontext.Insertable(new TestData()
                {
                    DocNum = txtdanhao.Text,
                    SeamSlippage = txtjiefenghuayi.Text,
                    TearStrength = txtshilieqiangdu.Text,
                    WaterproofEffect = txtfangshuixiaoguo.Text,
                    Weight = txtchengpingkezhong.Text,
                    Width = txtmenfu.Text,
                    Divest = txttestboli.Text,
                    Hydraulic = txttestshuiya.Text,
                    MoisturePermeability = txttesttoushi.Text,
                    Washing = txttestshuixi.Text
                }).ExecuteCommand();
                Connect.dbcontext.CommitTran();
                AlterDlg.Show("保存成功！");
                Init();
            }
            catch (Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                Tools.ShowMessageBox.ShowErrorBox("保存单据的时候发生了错误！" + ex.Message);
            }
        }

        private void 打印编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (后整理管理系统.自定义类.PrintService.CheckPrintDesignAccess())
            {
                Print(PrintModel.Design);
            }
        }
        private void Print(PrintModel c)
        {
            if (!DocmentAssignment())
            {
                return;
            }
            gridView1.CloseEditor();
            var lianxiren = LXRService.GetOneLXR(x => x.MC == txtkehu.Text);
            if (string.IsNullOrWhiteSpace(lianxiren.MC))
            {
                Sunny.UI.UIMessageBox.Show($"该客户{txtkehu.Text}名称在客户资料中没有匹配到！\r\n请检查输入的客户名是否正确");
                return;
            }
            var danju = new DanjuTable()
            {
                dh = txtdanhao.Text,
                rq = dateEdit1.DateTime,
                ksbh = Kehu.BH,
                ksmc = txtkehu.Text,
                lianxidianhua = txtlianxidianhua.Text,
                lianxiren = txtlianxiren.Text,
                djlx = DanjuLeiXing.打样单,
                own = CurrentUser.user.YHBH,
                SaleMan = txtdayangyuan.Text,
                yaoqiu = txtbeizhu.Text,
                bz = txtbeizhu.Text,
                zhuangtai = "未审核",
                jiagongleixing = comleixing.Text
            };
            if (requirements == null)
            { requirements = new ProofingRequirements(); }
            requirements.DocNum = txtdanhao.Text;
            requirements.FrontOrBack = txtzhengfangmian.Text;
            requirements.Hydraulic = txtshuiya.Text;
            requirements.MoisturePermeability = txttoushi.Text;
            requirements.TestStandards = txtceshibiaozhun.Text;
            requirements.Trimming = txtqiebian.Text;
            requirements.Washing = txtshuixi.Text;
            requirements.WaterproofEffect = txtfangshuixiaoguo.Text;
            requirements.Weight = txtkezhong.Text;
            requirements.Width = txtmenfu.Text;
            requirements.Glue = txtjiaoshui.Text;
            requirements.TPU = txtdibu.Text;
            requirements.Sticktype = txtgunxing.Text;
            requirements.Temperature = txtwendu.Text;
            requirements.FitPressure = txttieheyali.Text;
            requirements.RewindingPressure = txtshoujuanyali.Text;
            requirements.RewindingTension = txtshoujuanzhangli.Text;
            requirements.HandFeel = txtshougang.Text;
            requirements.BottomWeight = txtBottomWeight.Text;
            requirements.DetectionMetrics = txtDetectionmetrics.Text;
            requirements.Other = txtOther.Text;
            requirements.SeamSlippage = txtSeamSlippage.Text;
            requirements.TearStrength = txtTearStrength.Text;
            requirements.TopWeight = TxtTopWeight.Text;
            var listhouzhenli = new List<ShengchandanHouzhengli>
            {
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "A", Value = checkBoxX1.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "B", Value = checkBoxX2.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "C", Value = checkBoxX3.Checked },
                new ShengchandanHouzhengli() { shengchandanhao = txtdanhao.Text, HouzhengliGongyi = "D", Value = checkBoxX4.Checked }
            };
            var yaoqius = new List<ShengChanDanHouZhengLiYaoQiu>();
            danju.TotalMishu = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList().Count();
            if (工艺单据ToolStripMenuItem.SelectedIndex == 0)
            {
                var result = new Tools.打印打样单后整理() { Requirements = requirements, colorTables = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList(), formInfo = new FormInfo() { FormName = "打样管理", GridviewName = "gridView1" }, DanjuTable = danju, houzhenglis = listhouzhenli, yaoqius = yaoqius };
                result.打印(PrintPath.报表模板 + "打样单.frx", c);
            }
            else
            {
                new Tools.打印发货单()
                {
                    danjuTable = danju,
                    danjumingxitables = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList(),
                    danjuinfo = new Tools.FormInfo() { FormName = "销售发货单查询", GridviewName = gridView1.Name },
                    mingxiinfo = new Tools.FormInfo() { FormName = this.Name, GridviewName = gridView1.Name }
                }.Print(PrintPath.报表模板 + "打样收款单.frx", c);
            }
            /// 打印后询问是否保存
            AskSave();
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print(PrintModel.Privew);
        }

        private void 直接打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print(PrintModel.Print);
        }

        private void colorbtn_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 色号选择();
            fm.ShowDialog();
            //ColorTables [gridView1.FocusedRowHandle].Yaoqiu  = fm.colorInfo.ColorName ;
            gridControl1.RefreshDataSource();
            gridView1.CloseEditor();
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }
        private void dateEdit1_DateTimeChanged(object sender, EventArgs e)
        {
          
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var linkman = new YuanGongTable() { Xingming = "" };
            var fm = new 员工选择() { linkman = linkman };
            fm.ShowDialog();
            txtdayangyuan.Text = fm.linkman.Xingming;
        }

        private void uiTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var linkman = new YuanGongTable() { Xingming = "" };
                var fm = new 员工选择() { linkman = linkman };
                fm.ShowDialog();
                txtdayangyuan.Text = fm.linkman.Xingming;
            }
        }

        private void ButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 品种选择();
            fm.ShowDialog();
            var i = gridView1.FocusedRowHandle;
            foreach (var pingzhong in fm.pingzhong)
            {
                danjumingxitables[i].bizhong = "人民币￥";
                danjumingxitables[i].Bianhao = pingzhong.bh;
                danjumingxitables[i].guige = pingzhong.gg;
                danjumingxitables[i].chengfeng = pingzhong.cf;
                danjumingxitables[i].pingming = pingzhong.pm;
                danjumingxitables[i].kezhong = pingzhong.kz;
                danjumingxitables[i].menfu = pingzhong.mf;
                danjumingxitables[i].danwei = "米";
                danjumingxitables[i].PingzhiYang = "无品质样";
                i++;
                if (i == danjumingxitables.Count - 1)
                    for (int j = 0; j < 30; j++)
                    {
                        danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = Convert.ToDateTime(dateEdit1.Text) });
                    }
            }
            gridView1.CloseEditor();
            gridControl1.RefreshDataSource();
        }

        private void ButtonEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var fm = new 品种选择() { Bianhao = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Bianhao").ToString() };
                fm.ShowDialog();
                var i = gridView1.FocusedRowHandle;
                foreach (var pingzhong in fm.pingzhong)
                {
                    danjumingxitables[i].bizhong = "人民币￥";
                    danjumingxitables[i].Bianhao = pingzhong.bh;
                    danjumingxitables[i].guige = pingzhong.gg;
                    danjumingxitables[i].chengfeng = pingzhong.cf;
                    danjumingxitables[i].pingming = pingzhong.pm;
                    danjumingxitables[i].kezhong = pingzhong.kz;
                    danjumingxitables[i].menfu = pingzhong.mf;
                    danjumingxitables[i].danwei = "米";
                    danjumingxitables[i].PingzhiYang = "无品质样";
                    i++;
                    if (i == danjumingxitables.Count - 1)
                        for (int j = 0; j < 30; j++)
                        {
                            danjumingxitables.Add(new danjumingxitable() { danhao = txtdanhao.Text, rq = Convert.ToDateTime(dateEdit1.Text) });
                        }
                }
                gridView1.CloseEditor();
                gridControl1.RefreshDataSource();
            }
        }

        private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridControl1.Focus();
            SendKeys.Send("^c");
        }

        private void 粘贴单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridControl1.Focus();
            SendKeys.Send("^v");
        }

        private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            try
            {
                danjumingxitables[gridView1.FocusedRowHandle].hanshuiheji = danjumingxitables[gridView1.FocusedRowHandle].hanshuidanjia * danjumingxitables[gridView1.FocusedRowHandle].chengpingmishu;
                txtheji.Text = danjumingxitables.Sum(x => x.hanshuiheji).ToString();
                gridControl1.RefreshDataSource();
            }
            catch
            {
                MessageBox.Show("请输入数字。计算错误");
            }
        }

        private void 打样预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintDanju(PrintModel.Privew);
        }
        /// <summary>
        /// 单据信息赋值
        /// </summary>
        /// <returns></returns>
        private Boolean DocmentAssignment()
        {
            gridView1.CloseEditor();
            try
            {
                var lianxiren = LXRService.GetOneLXR(x => x.MC == txtkehu.Text);
                if (string.IsNullOrWhiteSpace(lianxiren.MC))
                {
                    Sunny.UI.UIMessageBox.Show($"该客户{txtkehu.Text}名称在客户资料中没有匹配到！\r\n请检查输入的客户名是否正确");
                    return false;
                }
                else
                {
                    Kehu.BH = lianxiren.BH;
                }
                if (string.IsNullOrWhiteSpace(comleixing.Text))
                {
                    MessageBox.Show("请填写工艺类型！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                if (string.IsNullOrWhiteSpace(txtkehu.Text))
                {
                    MessageBox.Show("请选择或填写客户！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                danju.dh = txtdanhao.Text;
                danju.rq = dateEdit1.DateTime;
                danju.ksbh = Kehu.BH;
                danju.ksmc = txtkehu.Text;
                danju.lianxidianhua = txtlianxidianhua.Text;
                danju.lianxiren = txtlianxiren.Text;
                danju.djlx = DanjuLeiXing.打样单;
                danju.own = CurrentUser.user.YHBH;
                //客户名称
                danju.SaleMan = txtdayangyuan.Text;
                //类型
                danju.jiagongleixing = comleixing.Text;
                //合计费用
                danju.je = txtheji.Value;
                danju.Hanshui = comhanshui.Text;
                danju.Qiankuan = cmbqiankuan.Text;
                danju.zhuangtai = "未审核";
                danju.bz = txtbeizhu.Text;
                danju.yaoqiu = txtbeizhu.Text;
                danju.TotalMishu = danjumingxitables.Sum(x => x.chengpingmishu);
                return true;
            }
            catch (Exception ex)
            {
                Tools.ShowMessageBox.ShowAskBox(ex.Message);
                return false;
            }
        }


        private void PrintDanju(PrintModel useful)
        {
            if (useful == PrintModel.Design)
            {
                if (!PrintService.CheckPrintDesignAccess())
                {
                    return;
                }
            }

            if (!DocmentAssignment())
            {
                return;
            }
         
        }
        /// <summary>
        /// 打印后询问是否保存
        /// </summary>
        private void AskSave()
        {
            if (QueryTime.PrintRule == PrintRuleStyle.PrintFirst)
            {
                if (Tools.ShowMessageBox.ShowAskBox("是否直接保存单据？") == DialogResult.Yes)
                {
                    保存ToolStripMenuItem_Click(null, null);
                }
            }
        }

        private void 打印编辑ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (!PrintService.CheckPrintDesignAccess())
            {
                return;
            }
            PrintDanju(PrintModel.Design);
        }

        private void 直接打印ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PrintDanju(PrintModel.Print);
        }

        private void label21_Click(object sender, EventArgs e)
        {

        }
    }
}
